Page({
  data: {
    pageNo: 1,
    pageSize: 4,
    pageCount: 0,
    comments: [],
    starsArray: [0, 1, 2, 3, 4],
    allData: [] // 模拟的所有数据（等于接口返回的总数据）
  },

  // 模拟假数据作为“接口返回”
  generateFakeData() {
    const totalList = [];
    for (let i = 1; i <= 15; i++) {
      totalList.push({
        id: i,
        user: '匿名用户555',
        star: Math.floor(Math.random() * 5) + 1,
        content: `这是第 ${i} 条模拟评论，医生讲得特别好！`,
        date: '2025-08-01',
        time: '10:00',
        reply: '感谢您的肯定！'
      });
    }

    this.setData({
      allData: totalList,
      pageCount: Math.ceil(totalList.length / this.data.pageSize)
    }, () => {
      this.getComments(); // 初次加载第一页
    });
  },

  // 本地分页加载
  getComments() {
    wx.showNavigationBarLoading(); // 推荐使用顶部 loading

    const { pageNo, pageSize, allData } = this.data;
    const start = (pageNo - 1) * pageSize;
    const end = start + pageSize;
  
    const newList = allData.slice(start, end);
  
    this.setData({
      comments: this.data.comments.concat(newList)
    });
  
    wx.hideNavigationBarLoading();
    wx.stopPullDownRefresh();
  },

  onLoad() {
    const totalList = [];
    for (let i = 1; i <= 15; i++) {
      totalList.push({
        id: i,
        user: '匿名用户555',
        star: Math.floor(Math.random() * 5) + 1,
        content: `这是第 ${i} 条模拟评论，医生讲得特别好！`,
        date: '2025-08-01',
        time: '10:00',
        reply: '感谢您的肯定！'
      });
    }
  
    this.setData({
      allData: totalList,
      pageCount: Math.ceil(totalList.length / this.data.pageSize)
    }, () => {
      this.getComments();
    });
  },

  onPullDownRefresh() {
    this.setData({
      pageNo: 1,
      comments: []
    }, () => {
      this.getComments();
    });
  },

  onReachBottom() {
    if (this.data.pageNo >= this.data.pageCount) {
      wx.showToast({
        title: '没有更多数据了',
        icon: 'none'
      });
      return;
    }

    this.setData({
      pageNo: this.data.pageNo + 1
    }, () => {
      this.getComments();
    });
  }
});
